package com.example.myapplication.dialogs;

import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.webkit.WebView;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.myapplication.R;
import com.example.myapplication.base.UrlManager;

public class PrivacyDailog {

    @RequiresApi(api = Build.VERSION_CODES.M)
    public static void showDailog(final Context context, final Activity activity) {
        // 利用layoutInflater获得View
        final LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View popView = inflater.inflate(R.layout.yinsi_dialog, null);
        final PopupWindow window = new PopupWindow(popView,
                WindowManager.LayoutParams.MATCH_PARENT,
                WindowManager.LayoutParams.MATCH_PARENT);
        // 设置popWindow弹出窗体可点击
        window.setFocusable(true);
        window.setOutsideTouchable(true);
        // 实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        window.setBackgroundDrawable(dw);
        //设置popWindow的显示和消失动画
        window.setAnimationStyle(R.style.mypopwindow_anim_style);
        //更新显示信息
        final TextView tv_user_di_que = popView.findViewById(R.id.tv_user_di_que);
        final WebView wb_yinsi_dialog = popView.findViewById(R.id.wb_yinsi_dialog);
        wb_yinsi_dialog.loadUrl(UrlManager.Privacy);
        wb_yinsi_dialog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                App.initApplication().setTime(activity);
            }
        });
        wb_yinsi_dialog.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View view, int i, int i1, int i2, int i3) {
//                App.initApplication().setTime(activity);
            }
        });
//        final TextView tv_user_di_qu = popView.findViewById(R.id.tv_user_di_qu);
        final TextView tv_dialog_x = popView.findViewById(R.id.tv_dialog_x);
        tv_dialog_x.setOnClickListener(new View.OnClickListener() {//点击×
            @Override
            public void onClick(View v) {
                changeLightclose(activity);
                window.dismiss();
            }
        });
        RelativeLayout rl_user_ll = popView.findViewById(R.id.rl_user_ll);
//        tv_user_di_qu.setOnClickListener(new View.OnClickListener() {//点击取消
//            @Override
//            public void onClick(View v) {
//                changeLightclose(activity);
//                window.dismiss();
//            }
//        });
        tv_user_di_que.setOnClickListener(new View.OnClickListener() {//点击确认
            @Override
            public void onClick(View view) {
                changeLightclose(activity);
                window.dismiss();
            }
        });
        rl_user_ll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                InputMethodManager imm = (InputMethodManager)
                        context.getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
            }
        });

        // 在底部显示
        window.showAtLocation(activity.findViewById(R.id.root_layout),
                Gravity.BOTTOM, 0, 0);
        //恢复背景亮度
        changeLightShow(activity);

        //popWindow消失监听方法
        window.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                changeLightclose(activity);
                System.out.println("popWindow消失");
            }
        });
    }

    /**
     * 背景变暗,并增加渐变动画
     */
    public static void changeLightShow(Activity activity) {
        final ValueAnimator animation = ValueAnimator.ofFloat(1.0f, 0.6f);
        animation.setDuration(200);
        animation.start();
        final Window window = activity.getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                lp.alpha = (float) valueAnimator.getAnimatedValue();
                window.setAttributes(lp);
            }
        });
    }

    /**
     * 恢复背景,并增加渐变动画
     */
    public static void changeLightclose(Activity activity) {
        final ValueAnimator animation = ValueAnimator.ofFloat(0.6f, 1.0f);
        animation.setDuration(200);
        animation.start();
        final Window window = activity.getWindow();
        final WindowManager.LayoutParams lp = window.getAttributes();
        animation.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                lp.alpha = (float) valueAnimator.getAnimatedValue();
                window.setAttributes(lp);
            }
        });
    }

}
